package com.test.mooan.labama.bean_boom.process_boom;

import com.google.gson.JsonArray;
import com.test.mooan.labama.utils_boom.Stringutil_boom;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class ProcessData_boom {

    private ProcessData_boom(){}

    private String title;
    private String hint;
    private String paramName;
    private String action;
    private String isMust;
    private JsonArray values;
    private String paramNameState;
    private String paramNameCity;
    private String paramNameArea;
    private String paramNameBankName;
    private String paramNameBankCode;

    private ArrayList<String> relationships;
    private String[] bankNames;

    private ArrayList<JSONObject> valueJSONs;

    public String gTitle() {
        return Stringutil_boom.getSafeString(title);
    }

    public String gHint() {
        return Stringutil_boom.getSafeString(hint);
    }

    public String gParamName() {
        return Stringutil_boom.getSafeString(paramName);
    }

    public String gAction() {
        return Stringutil_boom.getSafeString(action);
    }

    public boolean isMustInput(){
        return !Stringutil_boom.getSafeString(isMust).equals("NO");
    }

    public void sRelationships(ArrayList<String> relationships) {
        this.relationships = relationships;
    }

    public void sBankNames(String[] bankNames) {
        this.bankNames = bankNames;
    }

    public String[] gTitlesOfValue(){

        if (relationships!=null) return relationships.toArray(new String[0]);

        if (bankNames!=null) return bankNames;

        int size = values.size();

        if (valueJSONs==null||valueJSONs.isEmpty()){
            valueJSONs = new ArrayList<>();
            for (int i=0;i<size;i++){
                try {
                    valueJSONs.add(new JSONObject(values.get(i).toString()));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        size = valueJSONs.size();
        String[] result = new String[size];

        for (int i=0;i<size;i++){
            result[i] = valueJSONs.get(i).optString("title");
        }

        return result;
    }

    public String gParamNameCity() {
        return Stringutil_boom.getSafeString(paramNameCity);
    }

    public String gParamNameBankName() {
        return Stringutil_boom.getSafeString(paramNameBankName);
    }

    public String gParamNameBankCode() {
        return Stringutil_boom.getSafeString(paramNameBankCode);
    }

    public String gValueOfTitle(String title){

        if (valueJSONs==null||valueJSONs.isEmpty()){
            valueJSONs = new ArrayList<>();
            for (int i=0;i<values.size();i++){
                try {
                    valueJSONs.add(new JSONObject(values.get(i).toString()));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        for (JSONObject valueJSON:valueJSONs){
            if (valueJSON.optString("title").equalsIgnoreCase(title)) return valueJSON.optString("value");
        }

        return "";
    }
}
